package plugin.bpce.Telas;

import java.util.Properties;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import plugin.bpce.Object.InterfaceCoppeer;
import plugin.bpce.modelo.ConfiguracaoModelo;

public class TelaConfiguracao extends ApplicationWindow implements SelectionListener, KeyListener{
	private Text txtPesoPontuacaoInicial, txtPesoPontuacaoAvaliacao, txtPesoPontuacaoVisualizacao,
	  txtPortaCOPPEER, txtTempoEsperaSincronizacao;
	private Button btConfirmar, btCancelar, btRefresh;
	private ConfiguracaoModelo configuracao;
	private Shell Tela;
	private String porta;
	private InterfaceCoppeer coppeer;
	
	public TelaConfiguracao(Shell parent, ConfiguracaoModelo configuracao, String porta) {		
		super(parent);		
		
		this.configuracao = configuracao;
		this.Tela = parent;
		this.coppeer = new InterfaceCoppeer();
		
		if (porta == null){
			this.porta = "";
		}
		else{
			this.porta = porta;
		}
	}
	
	protected Control createContents(Composite parent) {
		CLabel lblPesoPtoInicial, lblPesoPtoAvaliacao, lblPesoPtoVisualizacao, lblPortaCOPPEER, lblTempoEsperaSincronizacao;
		GridData LayoutbtExportar, LayoutEdit;
		GridLayout LayoutTela, LayoutSuperior, LayoutGrupoBotoes;
		Group main, GrupoSuperior, GrupoBotoes;		
		
		parent.getShell().setText("Configuration");
		
		LayoutTela = new GridLayout();
		LayoutTela.numColumns = 1;		
		
		main = new Group(parent, SWT.APPLICATION_MODAL);
		main.setLayoutData(new GridData(GridData.FILL_BOTH));		
		main.setLayout(LayoutTela);
		
		LayoutSuperior = new GridLayout();
		LayoutSuperior.numColumns = 2;		
		
		GrupoSuperior = new Group(main, SWT.NONE);
		GrupoSuperior.setLayout(LayoutSuperior);	
		GrupoSuperior.setText("Configuration");		
		
		LayoutEdit = new GridData();
		LayoutEdit.heightHint = -1;
		LayoutEdit.widthHint = 45;		
		
		lblPesoPtoInicial = new CLabel(GrupoSuperior, SWT.NONE);
		lblPesoPtoInicial.setText("Initial Points Weight:");
		
		txtPesoPontuacaoInicial = new Text(GrupoSuperior, SWT.BORDER);
		txtPesoPontuacaoInicial.setOrientation(33554432);
		txtPesoPontuacaoInicial.setLayoutData(LayoutEdit);		
		txtPesoPontuacaoInicial.addKeyListener(this);		
		
		lblPesoPtoAvaliacao = new CLabel(GrupoSuperior, SWT.NONE);
		lblPesoPtoAvaliacao.setText("Evaluation Points Weight:");
		
		txtPesoPontuacaoAvaliacao = new Text(GrupoSuperior, SWT.BORDER);
		txtPesoPontuacaoAvaliacao.setOrientation(33554432);
		txtPesoPontuacaoAvaliacao.setLayoutData(LayoutEdit);		
		txtPesoPontuacaoAvaliacao.addKeyListener(this);		
		
		lblPesoPtoVisualizacao = new CLabel(GrupoSuperior, SWT.NONE);
		lblPesoPtoVisualizacao.setText("View Points Weight:");
		
		txtPesoPontuacaoVisualizacao = new Text(GrupoSuperior, SWT.BORDER);
		txtPesoPontuacaoVisualizacao.setOrientation(33554432);
		txtPesoPontuacaoVisualizacao.setLayoutData(LayoutEdit);		
		txtPesoPontuacaoVisualizacao.addKeyListener(this);		
		
		lblPortaCOPPEER = new CLabel(GrupoSuperior, SWT.NONE);
		lblPortaCOPPEER.setText("COPPEER's port:");
		
		txtPortaCOPPEER = new Text(GrupoSuperior, SWT.BORDER);
		txtPortaCOPPEER.setOrientation(33554432);
		txtPortaCOPPEER.setLayoutData(LayoutEdit);
		txtPortaCOPPEER.setText(porta);
		txtPortaCOPPEER.addKeyListener(this);
		
		lblTempoEsperaSincronizacao = new CLabel(GrupoSuperior, SWT.NONE);
		lblTempoEsperaSincronizacao.setText("Sync Time-Out(ms):");
		
		txtTempoEsperaSincronizacao = new Text(GrupoSuperior, SWT.BORDER);
		txtTempoEsperaSincronizacao.setOrientation(33554432);
		txtTempoEsperaSincronizacao.setLayoutData(LayoutEdit);		
		txtTempoEsperaSincronizacao.addKeyListener(this);
				
		LayoutGrupoBotoes = new GridLayout();
		LayoutGrupoBotoes.numColumns = 3;		
		
		GrupoBotoes = new Group(main, SWT.NONE);
		GrupoBotoes.setLayout(LayoutGrupoBotoes);
		
		LayoutbtExportar = new GridData();
		LayoutbtExportar.horizontalAlignment = org.eclipse.swt.layout.GridData.END;		
		
		btConfirmar = new Button(GrupoBotoes, SWT.NONE);
		btConfirmar.setText("Save");
		btConfirmar.setLayoutData(LayoutbtExportar);		
		btConfirmar.addSelectionListener(this);
		
		btCancelar = new Button(GrupoBotoes, SWT.NONE);
		btCancelar.setText("Cancel");
		btCancelar.addSelectionListener(this);
		
		btRefresh = new Button(GrupoBotoes, SWT.NONE);
		btRefresh.setText("Refresh Information");
		btRefresh.addSelectionListener(this);		
		
		setaValores();
		
		return parent;
	}	
	
	private void setaValores() {
		boolean configCriada;
		
		configCriada = true;
		if (this.configuracao != null){
			configCriada = false;
			
			txtPesoPontuacaoInicial.setText(this.configuracao.getPesoPontuacaoInicial());
			txtPesoPontuacaoAvaliacao.setText(this.configuracao.getPesoPontuacaoAvaliacao());
			txtPesoPontuacaoVisualizacao.setText(this.configuracao.getPesoPontuacaoVisualizacao());						
		}
		
		txtPesoPontuacaoInicial.setEditable(configCriada);			
		txtPesoPontuacaoAvaliacao.setEditable(configCriada);			
		txtPesoPontuacaoVisualizacao.setEditable(configCriada);
	}

	public void widgetSelected(SelectionEvent e){
		Properties conf;		
		
		if (e.getSource().equals(btCancelar)){			
			this.close();
		}
		
		if (e.getSource().equals(btConfirmar)) {
			if (validaCampos()){
				conf = new Properties();
				conf.setProperty("pPtoIni", this.txtPesoPontuacaoInicial.getText());
				conf.setProperty("pPtoView", this.txtPesoPontuacaoVisualizacao.getText());
				conf.setProperty("pPtoAva", this.txtPesoPontuacaoAvaliacao.getText());				
				conf.setProperty("TimeOut", this.txtTempoEsperaSincronizacao.getText());
				
				this.coppeer.salvaConfiguracao(conf, this.txtPortaCOPPEER.getText());
				
				this.close();
			}
		}
		
		if (e.getSource().equals(btRefresh)) {
			if (this.txtPortaCOPPEER.getText().length() == 0){
				MessageDialog.openError(this.Tela, "BPCE", "The COPPEER's port is required.");				
			}					
			else{
				this.configuracao = this.coppeer.recuperaConfiguracao(this.txtPortaCOPPEER.getText());
				
				if (this.configuracao != null){
					setaValores();
					coppeer.alteraPorta(this.txtPortaCOPPEER.getText());
				}
				else{
					MessageDialog.openError(this.Tela, "BPCE", "The communication with the COPPEER failed.");					
				}
			}			
		}
	}
	
	private boolean validaCampos(){
		float pesoPtoInicial, pesoPtoAvaliacao, pesoPtoVisualizacao;
			
		if (this.txtPesoPontuacaoInicial.getText().length() == 0){
			MessageDialog.openError(this.Tela, "BPCE", "The field Initial Points Weight is required.");
			return false;
		}		
		else if (this.txtPesoPontuacaoAvaliacao.getText().length() == 0){
			MessageDialog.openError(this.Tela, "BPCE", "The field Evaluation Points Weight is required.");
			return false;
		}
		else if (this.txtPesoPontuacaoVisualizacao.getText().length() == 0){
			MessageDialog.openError(this.Tela, "BPCE", "The field View Points Weight is required.");
			return false;
		}
		else if (this.txtPortaCOPPEER.getText().length() == 0){
			MessageDialog.openError(this.Tela, "BPCE", "The field COPPEER's port is required.");
			return false;			
		}
		else if (this.txtTempoEsperaSincronizacao.getText().length() == 0){
			MessageDialog.openError(this.Tela, "BPCE", "The field Sync Time-out is required.");
			return false;			
		}
		
		pesoPtoInicial = Float.parseFloat(this.txtPesoPontuacaoInicial.getText());
		pesoPtoAvaliacao = Float.parseFloat(this.txtPesoPontuacaoAvaliacao.getText());
		pesoPtoVisualizacao = Float.parseFloat(this.txtPesoPontuacaoVisualizacao.getText());
		
		if ((pesoPtoInicial + pesoPtoAvaliacao + pesoPtoVisualizacao) != 1){
			MessageDialog.openError(this.Tela, "BPCE", "The sum of weights should be a one.");
			return false;			
		}
		
		return true;		
	}
	
	public void widgetDefaultSelected(SelectionEvent e){
		
	}

	public void keyPressed(KeyEvent arg0) {
		if ((arg0.character >= 'a') && (arg0.character <= 'z') && (arg0.character >= 'A') && 
			(arg0.character <= 'Z') && (arg0.character == ',') && (arg0.character == '!')){
			MessageDialog.openError(this.Tela, "BPCE", "Type only numbers.");
			((Text)arg0.getSource()).forceFocus();
		}
	}

	public void keyReleased(KeyEvent arg0) {
	}
}